clear all
set more off

global path "~/shared_space/dbaharc/BHOR-YugoslavianRefugees/"
qui do "${path}do_files/InitializeGlobals.do"

/*************/
/*Global part*/
/*************/
use "${path}data/GermanYUGMigration.dta", clear
drop if iso3=="DEU" | iso3=="YUG"

keep if year==1990 | year==2000 | year==2010

ren (iso3 product) (nation sitc2)
merge 1:1 nation sitc2 year using "${path}data/data_mig.dta", keep(master matched) nogen keepusing(mig_tot)
ren (nation sitc2) (iso3 product)
xtset countryproductid year

qui ds mig*, v(32)
local miglist = r(varlist)
foreach m of local miglist {
    qui replace `m' = 0 if mi(`m')
    qui g ln`m' = asinh(`m')
    //qui g ln`m' = log(`m')
}


/************************************/
/*** DEFINE VARIABLES AND GLOBALS ***/
/************************************/
global rhsoptions_cpcyfe a(countryproductid countryyearid) cluster(countryproductid)
//g lnexp=log(exp_xdeu)
//g lnexpplus1 = log(exp_xdeu+1)
//g asinhexp = asinh(exp_xdeu)


/*simply mutiply share to be between 0 and 100 instead of 0 and 1, for easier interpretaiotn of coefficients*/
g share_xdeu = expshare_xdeu*100

/*Total world exports by product and year to be used as control*/
foreach v in exp exp_xdeu {
    egen total`v' = sum(`v'), by(product year)
    g lntotal`v' = asinh(total`v')
    label var lntotal`v' "Global exp (asinh)"
}

label var lnexp "$ log(exp) $"
label var lnexpplus1 "$ log(exp) $"
label var asinhexp "$ asinh(exp) $"

/**************************/
/*** Summary Statistics ***/
/**************************/
/*
global sumstats_start \begin{threeparttable}[b] \begin{tabular}{l c c c c c}\hline\hline \bf Variable & \bf N & \bf Mean & \bf sd & \bf Min & \bf Max \\ \hline
global sumstats_end \hline\end{tabular} \begin{tablenotes} \item {\footnotesize \tablenote} \end{tablenotes} \end{threeparttable}

global sumstatvars lnexp lnexpplus1 asinhexp L10.mig_tot L10.mig_edu_mh L10.mig_edu_low L10.mig_col_w_1 L10.mig_col_w_0 L10.mig_task_analytical L10.mig_task_manual L10.mig_psf_amd_1 L10.mig_psf_amd_0
//open output file
cap file close fh
file open fh using "${path}/tables/SumStats.tex", write replace
//write table header to file
file write fh "$sumstats_start" _n
foreach i of global sumstatvars {
    qui su `i' if !mi(L10.mig_tot) & (year == 2000 | year==2010), d
    local obs = `r(N)'
    local mean = `r(mean)'
    local se = `r(sd)'
    local min = `r(min)'
    local max = `r(max)'
    local i = subinstr("`i'","L10.","",.)
    local varlabel: var label `i'
    file write fh "`varlabel'" "&" %9.0fc (`obs')  "&" %14.3fc (`mean')  "&" %14.2fc (`se')  "&" %14.1fc (`min')  "&" %14.1fc (`max') "\\"  _n
}
file write fh "$sumstats_end" _n
//close file handle
file close fh

label var lnmig_tot "AllMigrants"
label var lnmig_edu_mh "Skilled"
label var lnmig_edu_low "Unskilled"
label var lnmig_col_w_1 "WhiteCollars"
label var lnmig_col_w_0 "NonWhiteCollars"
label var lnmig_psf_amd_1 "HiProbSolving"
label var lnmig_psf_amd_0 "LoProbSolving"
label var lnmig_task_analytical "AnalyticalCognitive"
label var lnmig_task_manual "Manual"
label var lnmig_cert_amd_1 "Certified"
label var lnmig_cert_amd_0 "Non-certified"
*/

/****************************************/
/*** Regressions: Tables and Figures  ***/
/****************************************/
/*All Sample DiffInDiff*/
foreach v in lnexp lnexpplus1 asinhexp {
    local lbl`v' : variable label `v'
    eststo, title("`lbl`v''"): qui reghdfe `v' L10.lnmig_tot lntotalexp if year==2000 | year==2010, $rhsoptions_cpcyfe
    qui estadd local cpfe "Y"
    //eststo, title("`v' 2SLS"): qui reghdfe `v'  lntotalexp (L10.lnmig_tot==L10.lnmig_tot_IV) if year==2000 | year==2010, $rhsoptions_cpcyfe
    //qui estadd local cpfe "Y"
}
estout, $estout_params_nostats_txt $estout_stats_cpfe rename(L10.lnmig_tot "Migrants (asinh, lagged)")
estout using "${path}tables/AllSampleDID.tex", replace $estout_params_nostats $estout_stats_cpfe $estout_pre $estout_post rename(L10.lnmig_tot "Migrants (asinh, lagged)")
eststo clear
